from flask import Flask, render_template
import csv
from flask import request
from sklearn.linear_model import Ridge, Lasso, ElasticNetCV
from sklearn.model_selection import GridSearchCV, KFold
import numpy as np
import pandas as pd

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

def judg(t):
    if t=='None':
        return 0
    else:
        return 1

@app.route('/app/a', methods=['POST', 'GET'])
def a():
    if request.method == 'POST':
        are=request.form.get('area')
        ta1=judg(str(request.form.get('tag1')))
        ta2 =judg(str(request.form.get('tag2')))
        ta3 = judg(str(request.form.get('tag3')))
        ta4 = judg(str(request.form.get('tag4')))
        ta5 = judg(str(request.form.get('tag5')))
        ta6 = judg(str(request.form.get('tag6')))
        ta7 = judg(str(request.form.get('tag7')))
        ta8 = judg(str(request.form.get('tag8')))
        fea = [are, ta1, ta2, ta3, ta4, ta5, ta6, ta7, ta8]
        x_1 = np.array([fea])

        money = np.array(pd.read_csv("./pycode/yuce.csv", header=0).values)
        # money_1 = np.array(pd.read_csv("./pycode/yuce_test.csv").values)
        x = money[:, :9]
        y = money[:, 9]

        fold = 10
        yucezhi = []
        for i in range(10):
            # print("i=", i, " start")
            # y_test_total, y_pred_total = Kflodtrain(x, y,x_1 ,fold, "LASSO")
            kfold = KFold(n_splits=fold, shuffle=True)
            y_test_total, y_pred_total = [], []
            for train_index, test_index in kfold.split(x, y):
                x_train, y_train = x[train_index], y[train_index]
                x_test = x_1
                param_grid = {'alpha': [5]}  # , 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1,0.0005
                model = GridSearchCV(Lasso(), param_grid, cv=10).fit(x_train, y_train)
                y_pred = model.predict(x_test)
                yucezhi.append(y_pred)
        a = np.array(yucezhi)
        b=int(a.mean())
        if b<0:
            return '屁大的房子还想住？？？'
        else:
            m = str(b)
            return "我们为您推荐的价格是： {0}   元/月".format(m)

        # return '{0} {1} {2} {3} {4} {5} {6} {7} {8}'.format(x_test[0],x_test[1],x_test[2],x_test[3],x_test[4],x_test[5],x_test[6],x_test[7],x_test[8])
    return render_template('index.html')


@app.route('/app/yuce', methods=['POST', 'GET'])
def yuce():
    if request.method == 'POST':
        money = np.array(pd.read_csv("./pycode/yuce.csv", header=0).values)
        money_1 = np.array(pd.read_csv("./pycode/yuce_test.csv").values)
        x = money[:, :9]
        y = money[:, 9]
        x_1 = money_1[:1, :9]
        fold = 10
        yucezhi = []
        for i in range(10):
            # print("i=", i, " start")
            # y_test_total, y_pred_total = Kflodtrain(x, y,x_1 ,fold, "LASSO")
            kfold = KFold(n_splits=fold, shuffle=True)
            y_test_total, y_pred_total = [], []
            for train_index, test_index in kfold.split(x, y):
                x_train, y_train = x[train_index], y[train_index]
                x_test = x_1
                param_grid = {'alpha': [5]}  # , 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1,0.0005
                model = GridSearchCV(Lasso(), param_grid, cv=10).fit(x_train, y_train)
                y_pred = model.predict(x_test)
                yucezhi.append(y_pred)
        a = np.array(yucezhi)
        m = str(int(a.mean()))
        return "我们为您推荐的价格是： " + m
    return render_template('index.html')


if __name__ == '__main__':
    app.run()
